﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.GUI;
using System.Windows.Forms;
using ITAP.ContourBuilder.Components;
using System.Drawing;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandEnterText : Command
    {
        public CommandEnterText(ContourControl control)
            : base(control)
        {
        }

        ContourText contourText;
        public override bool AllowMultipleInstances
        {
            get
            {
                return false;
            }            
        }

        public override void DrawOnGraphics(System.Drawing.Graphics g, int screenWidth, int screenHeight, System.Drawing.RectangleF modelRectangle, ITAP.Core.Modelling.ModelPoint cameraCenter, ITAP.Core.Calculations.ModelCoordinateConverter coordinateConverter)
        {
            base.DrawOnGraphics(g, screenWidth, screenHeight, modelRectangle, cameraCenter, coordinateConverter);
            if (contourText == null)
                return;
            brush = new SolidBrush(contourText.FontColor);
            PointF screenPoint = this.ModelPointToScreenPoint(contourText.Position);


            if (contourText.TextType == TextRenderType.UseScreenTextSize)
                g.DrawString(contourText.Text, contourText.Font, brush, screenPoint);
            else
            {
                Font modelFont = new Font(contourText.Font.FontFamily, contourText.Font.Size * (float)GlobalData.FontSizeToScreenSizeFactor * (float)coordinateConverter.Scale, contourText.Font.Style);
                g.DrawString(contourText.Text, modelFont, brush, screenPoint);
            }
            contourText.StringSizeFromRender = g.MeasureString(contourText.Text, contourText.Font);
        }

        private SolidBrush brush;

        public override void Execute()
        {
            base.Execute();

            //contourText = ContourElementFactory.CreateText(form.EnteredText, new Font("Arial", 10), Color.Black);
            //brush = new SolidBrush(Color.Gray);
            //prevMousePoint = this.control.PointToClient(Cursor.Position);
            //ModelPoint startPoint = this.ScreenPointToModelPoint(prevMousePoint);
            //contourText.MoveTo(startPoint);
        }

        //private Point prevMousePoint;
        private FormEnterText form;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                form = new FormEnterText();
                this.disableObtainPeripheryState = true;
                form.FormClosed += new FormClosedEventHandler(form_FormClosed);
                form.TextDialogChanged += new TextDialogChangedDelegate(form_TextDialogChanged);
                contourText = ContourElementFactory.CreateText(form.EnteredText, form.TextFont, form.TextColor);
                brush = new SolidBrush(form.TextColor);
                ModelPoint startPoint = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                contourText.MoveTo(startPoint);
                form.ShowDialog();
              //  this.Exit();
            }
        }

        void form_TextDialogChanged()
        {
            contourText.FontColor = form.TextColor;
            contourText.Font = form.TextFont;
            contourText.Text = form.EnteredText;
            contourText.TextType = form.TextType;
            this.control.Invalidate();
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.disableObtainPeripheryState = false;
            if (form.DialogResult == DialogResult.OK)
            {
                this.control.ContourContainer.AddInnerComponent(contourText);
                contourText = null;
                this.control.ToleranceManager.Visit(this.control.ContourContainer);
                this.control.UpdateModel();
                this.Exit();
            }
            else
                this.Cancel();
            contourText = null;
        }

        public override string ToString()
        {
            return "Ввод текста";
        }
    }
}
